﻿using EFCoreDemo.Database;
using EFCoreDemo.Entity;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace EFCoreDemo.Repository
{
    public class BaseQueryable<T> : IQueryRepository<T> where T : class, IQuery
    {
        protected readonly DatabaseContext context;

        private IQueryable<T> queryable;
        public BaseQueryable(DatabaseContext context)
        {
            this.context = context;
        }
        public IQueryable<T> Queryable 
        { 
            get
            {
                if (queryable == null)
                {
                    queryable = context.Set<T>().AsNoTracking();
                }
                return queryable;
            }
        }

        public Type ElementType => Queryable.ElementType;

        public Expression Expression => Queryable.Expression;

        public IQueryProvider Provider => Queryable.Provider;

        public IEnumerator<T> GetEnumerator() => Queryable.GetEnumerator();

        IEnumerator IEnumerable.GetEnumerator() => Queryable.GetEnumerator();
    }
}
